package com.tp;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class ChooseLocation extends MapActivity implements OnClickListener,
		LocationListener {
	/** Called when the activity is first created. */
	LocationManager lm = null;
	EditText txted = null;
	Location location;
	Button btnSimple = null;

	MapView gMapView = null;

	MapController mc = null;

	Drawable defaultMarker = null;

	GeoPoint p = null;
	GeoPoint p2 = null;

	static double latitude;
	static double longitude;
	boolean gps_enabled = false;
	boolean network_enabled = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_choose_location);

		// name = Add.objName.getText().toString();

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
		network_enabled = lm
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		if (!gps_enabled && !network_enabled) {
			Toast.makeText(this, "GPS va NetWork khong enable",
					Toast.LENGTH_SHORT).show();
		}
		if (network_enabled) {
			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
					this);
			Toast.makeText(this, "NetWork is enable",
					Toast.LENGTH_SHORT).show();
		}
		if (gps_enabled){
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
			Toast.makeText(this, "GPS is enable",
					Toast.LENGTH_SHORT).show();
		}

		// Creating and initializing Map
		gMapView = (MapView) findViewById(R.id.map_id);
		p = new GeoPoint((int) (latitude * 1000000),
				(int) (longitude * 1000000));
		// p2 = new GeoPoint((int)(21.028903f*1000000),(int)
		// (105.849515f*1000000) );
		gMapView.setSatellite(true);
		mc = gMapView.getController();
		mc.setCenter(p);
		mc.setZoom(14);

		// Add a location mark
		MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
		List<Overlay> list = gMapView.getOverlays();
		list.add(myLocationOverlay);

		// Adding zoom controls to Map
		ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
		zoomControls.setLayoutParams(new ViewGroup.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		gMapView.addView(zoomControls);
		gMapView.displayZoomControls(true);

		// Getting locationManager and reflecting changes over map if distance
		// travel by
		// user is greater than 500m from current location.
		// finish();
	}

	/* This method is called when use position will get changed */

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	/* User can zoom in/out using keys provided on keypad */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_I) {
			gMapView.getController().setZoom(gMapView.getZoomLevel() + 1);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_O) {
			gMapView.getController().setZoom(gMapView.getZoomLevel() - 1);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_S) {
			gMapView.setSatellite(true);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_T) {
			gMapView.setTraffic(true);
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Add.lati = (float) (p.getLatitudeE6() / 1E6);
			Add.longi = (float) (p.getLongitudeE6() / 1E6);
			Intent a = new Intent(this, Add.class);
			// a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(a);
			return true;
		}
		/*
		 * else if(keyCode == KeyEvent.KEYCODE_BACK){ startActivity(new
		 * Intent(this,LocationDemo.class)); return true; }
		 */
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	/* Class overload draw method which actually plot a marker,text etc. on Map */
	protected class MyLocationOverlay extends com.google.android.maps.Overlay {

		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			Paint paint = new Paint();

			super.draw(canvas, mapView, shadow);
			// Converts lat/lng-Point to OUR coordinates on the screen.
			Point myScreenCoords = new Point();
			mapView.getProjection().toPixels(p, myScreenCoords);
			// Point point = new Point();
			// mapView.getProjection().toPixels(p2, point);
			paint.setStrokeWidth(1);
			// paint.setARGB(255, 255, 255, 255);
			paint.setColor(Color.BLUE);
			paint.setStyle(Paint.Style.STROKE);

			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.b);
			// display many makers in map
			canvas.drawBitmap(bmp, myScreenCoords.x - bmp.getWidth() / 2,
					myScreenCoords.y - bmp.getHeight(), paint);
			canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y,
					paint);
			// canvas.drawLine(myScreenCoords.x, myScreenCoords.y, point.x,
			// point.y, paint);
			// stopY, paint)
			return true;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.android.maps.Overlay#onTap(com.google.android.maps.GeoPoint
		 * , com.google.android.maps.MapView)
		 */
		@Override
		public boolean onTap(GeoPoint point, MapView mapView) {
			// TODO Auto-generated method stub
			Context contexto = mapView.getContext();
			String msg = "Lat: " + point.getLatitudeE6() / 1E6 + " - "
					+ "Lon: " + point.getLongitudeE6() / 1E6;
			p = point;
			Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
			toast.show();

			return true;
		}

	}

	@Override
	public void onLocationChanged(Location loc) {
		// TODO Auto-generated method stub

		/*
		 * p = new GeoPoint((int) latitude * 1000000, (int) longitude *
		 * 1000000); mc.animateTo(p);
		 */
		lm.removeUpdates(this);
		/*
		 * latitude = (float) loc.getLatitude(); longitude = (float)
		 * loc.getLongitude();
		 */
		// location = loc;

		if (loc.getLatitude() == 0 || loc.getLongitude() == 0) {
		} else {
			latitude = loc.getLatitude();
			longitude = loc.getLongitude();
			p = new GeoPoint((int) (latitude * 1000000),
					(int) (longitude * 1000000));
			mc.setCenter(p);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.location.LocationListener#onProviderDisabled(java.lang.String)
	 */
	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.location.LocationListener#onProviderEnabled(java.lang.String)
	 */
	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.location.LocationListener#onStatusChanged(java.lang.String,
	 * int, android.os.Bundle)
	 */
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}